import React from "react"
import {compose, lifecycle, withHandlers, withState} from "recompose"
import {del, get, postJSON, postParam, putJSON, putParam} from "server"
import {DEV, env, pageSetting, orderType} from "project-config"
import {rootPath} from "../../../../shared/init"

const makeRequestCompose = compose(
  withState('dataSource', '$dataSource', []),
  withState('loading', '$loading', false),
  withState('pagination', '$pagination', Object.assign({}, pageSetting)),
  withState('searchKey', '$searchKey', ''),
  withHandlers({
    getPageData: ({$pagination, pagination, $loading, $dataSource}) => {
      return (param, pageSize, pageNum) => {
        $loading(true)
        get(`/order/makeRequest/page/${pageSize || 10}/${pageNum || 1}`, param)
          .then(page => {
            $dataSource(page.pageData)
            pagination.total = page.total
            pagination.pageSize = page.pageSize
            pagination.current = page.pageNum
            $pagination(pagination)
          }).finally(() => {
          $loading(false)
        })
      }
    }
  }),
  withHandlers({
    onFastSearch: ({$searchKey, getPageData}) => {
      return (key) => {
        $searchKey(key)
        getPageData({searchKey: key})
      }
    }
  }),
  withHandlers({
    onPageChange: ({searchKey, getPageData}) => {
      return (page, filter, sorter) => {
        const searchParam = {searchKey: searchKey || ''}
        if (sorter && sorter.columnKey) {
          searchParam.queryOrder = {column: sorter.columnKey, orderType: orderType[sorter.order]}
        }
        getPageData(searchParam, page.pageSize, page.current)
      }
    },
    onView: ({history}) => {
      return (row) => {
        history.push(`/${rootPath}/clothing/detail/${row.clothingId}`)
      }
    }
  }),
  lifecycle({
    componentDidMount() {
      const {getPageData} = this.props
      getPageData()
    }
  })
)

export default makeRequestCompose